package com.zenithmind.mall.pojo.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 订单物流信息实体 - 遵循单一职责原则，专门管理物流信息
 * 
 * @author ZenithMind Team
 * @since 2025-07-03
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("mall_order_shipping")
public class OrderShipping extends BaseEntity {

    /**
     * 订单ID
     */
    private String orderId;

    /**
     * 物流公司
     */
    private String shippingCompany;

    /**
     * 物流单号
     */
    private String shippingNo;

    /**
     * 物流状态（0-未发货，1-已发货，2-运输中，3-派送中，4-已签收，5-异常）
     */
    private Integer shippingStatus;

    /**
     * 发货时间
     */
    private LocalDateTime shipTime;

    /**
     * 收货时间
     */
    private LocalDateTime receiveTime;

    /**
     * 预计送达时间
     */
    private LocalDateTime estimatedDeliveryTime;

    /**
     * 物流备注
     */
    private String shippingRemark;

    /**
     * 物流异常原因
     */
    private String exceptionReason;

    /**
     * 签收人
     */
    private String signedBy;

    /**
     * 最后更新时间
     */
    private LocalDateTime lastUpdateTime;

    // 业务方法

    /**
     * 检查是否未发货
     */
    public boolean isNotShipped() {
        return Objects.equals(this.shippingStatus, 0);
    }

    /**
     * 检查是否已发货
     */
    public boolean isShipped() {
        return Objects.equals(this.shippingStatus, 1);
    }

    /**
     * 检查是否运输中
     */
    public boolean isInTransit() {
        return Objects.equals(this.shippingStatus, 2);
    }

    /**
     * 检查是否派送中
     */
    public boolean isOutForDelivery() {
        return Objects.equals(this.shippingStatus, 3);
    }

    /**
     * 检查是否已签收
     */
    public boolean isDelivered() {
        return Objects.equals(this.shippingStatus, 4);
    }

    /**
     * 检查是否有异常
     */
    public boolean hasException() {
        return Objects.equals(this.shippingStatus, 5);
    }

    /**
     * 标记为已发货
     */
    public void markAsShipped(String company, String trackingNo) {
        this.shippingStatus = 1;
        this.shippingCompany = company;
        this.shippingNo = trackingNo;
        this.shipTime = LocalDateTime.now();
        this.lastUpdateTime = LocalDateTime.now();
    }

    /**
     * 更新为运输中
     */
    public void updateToInTransit() {
        if (isShipped()) {
            this.shippingStatus = 2;
            this.lastUpdateTime = LocalDateTime.now();
        }
    }

    /**
     * 更新为派送中
     */
    public void updateToOutForDelivery() {
        if (isInTransit()) {
            this.shippingStatus = 3;
            this.lastUpdateTime = LocalDateTime.now();
        }
    }

    /**
     * 标记为已签收
     */
    public void markAsDelivered(String signedBy) {
        if (isOutForDelivery()) {
            this.shippingStatus = 4;
            this.signedBy = signedBy;
            this.receiveTime = LocalDateTime.now();
            this.lastUpdateTime = LocalDateTime.now();
        }
    }

    /**
     * 标记为异常
     */
    public void markAsException(String reason) {
        this.shippingStatus = 5;
        this.exceptionReason = reason;
        this.lastUpdateTime = LocalDateTime.now();
    }

    /**
     * 检查是否有有效的物流单号
     */
    public boolean hasValidTrackingNumber() {
        return shippingNo != null && !shippingNo.trim().isEmpty();
    }

    /**
     * 检查是否可以查询物流
     */
    public boolean canTrack() {
        return hasValidTrackingNumber() && 
               shippingCompany != null && !shippingCompany.trim().isEmpty() &&
               (isShipped() || isInTransit() || isOutForDelivery() || isDelivered());
    }

    /**
     * 获取物流状态名称
     */
    public String getShippingStatusName() {
        if (shippingStatus == null) return "未知";
        
        switch (shippingStatus) {
            case 0: return "未发货";
            case 1: return "已发货";
            case 2: return "运输中";
            case 3: return "派送中";
            case 4: return "已签收";
            case 5: return "异常";
            default: return "未知";
        }
    }

    /**
     * 计算运输时长（小时）
     */
    public Long getTransitHours() {
        if (shipTime == null) {
            return null;
        }
        
        LocalDateTime endTime = receiveTime != null ? receiveTime : LocalDateTime.now();
        return java.time.Duration.between(shipTime, endTime).toHours();
    }

    /**
     * 检查是否超时
     */
    public boolean isOverdue() {
        if (estimatedDeliveryTime == null || isDelivered()) {
            return false;
        }
        
        return LocalDateTime.now().isAfter(estimatedDeliveryTime);
    }

    /**
     * 设置预计送达时间（根据发货时间和预计天数）
     */
    public void setEstimatedDeliveryTime(int estimatedDays) {
        if (shipTime != null && estimatedDays > 0) {
            this.estimatedDeliveryTime = shipTime.plusDays(estimatedDays);
        }
    }

    /**
     * 验证物流信息数据完整性
     */
    public boolean isValid() {
        return orderId != null && !orderId.trim().isEmpty() &&
               shippingStatus != null && shippingStatus >= 0 && shippingStatus <= 5;
    }

    /**
     * 检查物流信息是否完整
     */
    public boolean isComplete() {
        return hasValidTrackingNumber() && 
               shippingCompany != null && !shippingCompany.trim().isEmpty() &&
               shipTime != null;
    }

    /**
     * 获取物流进度百分比
     */
    public int getProgressPercentage() {
        if (shippingStatus == null) return 0;
        
        switch (shippingStatus) {
            case 0: return 0;   // 未发货
            case 1: return 25;  // 已发货
            case 2: return 50;  // 运输中
            case 3: return 75;  // 派送中
            case 4: return 100; // 已签收
            case 5: return 0;   // 异常
            default: return 0;
        }
    }
}
